#include "comM.hpp"
#include "log.hpp"
#include <iostream>
#include <cstdio>
#include <unistd.h>

extern Log log; // 开启日志

int main()
{
    // 获取并链接共享内存
    int shmid = GetShm();
    char *shmaddr = (char *)shmat(shmid, nullptr, 0);

    // 打开命名管道
    int fd = open(FIFO_FILE,O_WRONLY);
    if (fd < 0)
    {
        log(Fatal, "open errno: %s, errno code: %d", strerror(errno), errno);
        exit(FIFO_OPEN_ERR);
    }

    // IPC Code
    while (true)
    {
        std::cout << "Please say@ ";
        fgets(shmaddr, SIZE, stdin);  // 直接访问共享内存
        write(fd, "y", 1);  // 通知对方
    }

    shmdt(shmaddr);
    shmctl(shmid, IPC_RMID, nullptr);  // ipcrm -m
    close(fd);

    return 0;
}